// part1:C10:ex10.33.cpp
// use istream_iterator to read from a file.
// use ostream_iterator to write odd number to a output file.
// use ostream_iterator to write even number to a output file.
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;

int main(int argc, char *argv[]) {
  if (argc != 4) {
    cout << "usage: execise.txt in_file " 
      "out_file1 out_file2" << endl; 
    return -1;
  }

  ifstream in(argv[1]);
  if (!in) {
    cout << "failed open input file!" << endl;
    exit(1);
  }

  ofstream out1(argv[2]);
  if (!out1) {
    cout << "failed open output file 1!" << endl; 
    exit(1);
  }
  ofstream out2(argv[3]);
  if (!out2) {
    cout << "failed open output file 2!" << endl;
    exit(1);
  }

  istream_iterator<int> in_iter(in);
  istream_iterator<int> eof;
  ostream_iterator<int> out_iter1(out1, " ");
  ostream_iterator<int> out_iter2(out2, "\n");
  
  while (in_iter != eof) {
    if (*in_iter & 1) {
      *out_iter1++ = *in_iter;
    } else {
      *out_iter2++ = *in_iter;
    }
    in_iter++;
  }
  
  return 0;
}
